package demo01_c3p0的使用_硬编码;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import demo01_JDBC入门.User;
import demo05_JDBC工具类的抽取.JBDCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class test {
    public static void main(String[] args) throws  Exception{
        //创建C3P0连接池对象
        ComboPooledDataSource pools=new ComboPooledDataSource();
        //设置连接池参数
        pools.setDriverClass("com.mysql.jdbc.Driver");
        pools.setJdbcUrl("jdbc:mysql://localhost:3306/day19_1");
        pools.setUser("root");
        pools.setPassword("root");
        //获得连接
        Connection connection = pools.getConnection();
        //预编译sql语句,得到预编译对象
        String sql="select *from user where id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        //设置sql语句参数
        ps.setInt(1,2);
        //执行sql语句,处理结果
        ResultSet resultSet = ps.executeQuery();
        //定义一个user变量
        User user=null;
        while (resultSet.next()){
            //取值
            int id = resultSet.getInt("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            String nickname = resultSet.getString("nickname");
            //封装数据
            user=new User(id,username,password,nickname);
            System.out.println(user);
        }
        //释放资源
        JBDCUtils.release(resultSet,ps,connection);
    }
}
